<?php
    require_once ("Includes/session.php");
    require_once ("Includes/simplecms-config.php"); 
    require_once ("Includes/connectDB.php");
    require_once("conf.php");   
    include("Includes/header.php"); 
    //check if admin
    confirm_is_admin();//this should be enough

    if(isset($_POST['Add']))
    {
        $question = trim($_POST ['questionText']);
        $C1 = trim($_POST['choice1']);
        $C2 = trim($_POST['choice2']);
        $C3 = trim($_POST['choice3']);
        $C4 = trim($_POST['choice4']);
        $CC = trim($_POST['correctChoice']);

        $EID = $_POST['Exam'];

        

        if(empty($question) || empty($C1) || empty($C2) || empty($C3) || empty($C4) || empty($CC) || empty($EID))
        {

            echo "<script type='text/javascript'>alert('All fields are required!!! ')</script>";

        }
        elseif($CC != 1 && $CC != 2 && $CC != 3 && $CC != 4)
        {
            echo "<script type='text/javascript'>alert('The correct choice ID must be 1 or 2 or 3 or 4')</script>";
        }
        else
        {
            $fullexam = "SELECT * FROM questions WHERE exam_id = ?";

            $fexam = $databaseConnection->prepare($fullexam);
            $fexam->bind_param('d',$EID);
            $fexam->execute();
            $fexam->store_result();
            $num = $fexam->num_rows;
            if($fexam->num_rows >= 30)
            {
                echo "<script type='text/javascript'>alert('This exam is full - it already has 30 questions-')</script>";
            }
            else
            {

                $query = "INSERT INTO Questions (question,C1,C2,C3,C4,AIndex,exam_id) VALUES (?,?,?,?,?,?,?)";

                $statement = $databaseConnection->prepare($query);
                $statement->bind_param('sssssds', $question , $C1 , $C2 , $C3 , $C4 , $CC , $EID);
                $statement->execute();
                $statement->store_result();
                $creationWasSuccessful = $statement->affected_rows == 1 ? true : false;
                if ($creationWasSuccessful)
                {
                    echo "<script type='text/javascript'>alert('Creation Successful')</script>";
                    if($num == 29)
                    {
                        $query = "UPDATE exams SET ready = 1 WHERE id = ?";
                        $stmt = $databaseConnection->prepare($query);
                        $stmt->bind_param('s', $EID);
                        $stmt->execute();
                        $stmt->store_result();
                        $updateWasSuccessful = $stmt->affected_rows == 1 ? true : false;
                        if($updateWasSuccessful)
                        {
                            echo "<script type='text/javascript'>alert('Exam is ready - has 30 questions-')</script>";
                        }
                        else
                        {
                            echo "<script type='text/javascript'>alert('Exam could not be flagged!!!')</script>";    
                        }
                    }
                }
                else
                {
                    echo "<script type='text/javascript'>alert('An Error occured !')</script>";
                }
                $statement->close();
            }            
        }

    } 



?>
        <script type="text/javascript">
            var lastActiveArea;

            function updateLastActiveArea(element) {
                lastActiveArea = element;
            }

        </script>
        <div id="add" style="width: 30%; height: 500px;margin-right: 5%;">
            <h2>Add Question</h2>
                <form action="Admin_Questions.php" method="post">
                    <fieldset>
                    <legend>Add Question</legend>
                    <ol>
                        <li>
                            <label for="questionText">Text:</label> 
                            <textarea name="questionText" id="questionText" required="" cols="35" rows="5" onfocus="updateLastActiveArea(this)"> </textarea>
                        </li>
                        <li>
                            <label for="choice1">Choice 1:</label> 
                            <textarea name="choice1" id="choice1" required="" cols="35" rows="5" onfocus="updateLastActiveArea(this)"> </textarea>
                        </li>
                        <li>
                            <label for="choice2">Choice 2:</label> 
                            <textarea name="choice2" id="choice2" required="" cols="35" rows="5" onfocus="updateLastActiveArea(this)"> </textarea>
                        </li>
                        <li>
                            <label for="choice3">Choice 3:</label> 
                            <textarea name="choice3" id="choice3" required="" cols="35" rows="5" onfocus="updateLastActiveArea(this)"> </textarea>
                        </li>
                        <li>
                            <label for="choice4">Choice 4:</label> 
                            <textarea name="choice4" id="choice4" required="" cols="35" rows="5" onfocus="updateLastActiveArea(this)"> </textarea>
                        </li>
                        <li>
                            <label for="correctChoice">Correct Choice ID (1 or 2 or 3 or 4):</label> 
                            <input type="text" name="correctChoice" id="correctChoice" required="" />
                        </li>
                        <li>
                            <label for="Exam">Exam:</label> 
                            <select name = 'Exam' id='Exam' style='width: 200px;'>
                            <?php
                                $query = "SELECT id, title From exams";
                                $res = $databaseConnection->query($query);
                                while (($row = $res->fetch_row()) != NULL)
                                {
                                    echo "<option value = '{$row[0]}'";
                                    echo ">{$row[1]}</option>";
                                }
                            ?>
                            </select>
                        </li>
                    </ol>
                    <input type="submit" name="Add" value="Add" />
                    <p>
                        <a href="Admin_Questions.php">Cancel</a>
                    </p>
                </fieldset>
            </form>
        </div>

        <div id="edit" style="padding: 10px 10px 10px 10px;">
            <h2>Add Signs</h2>
            <p>Double click a sign to add. It will be added to the last selected text area.</p>    
            <?php

            $dg = new C_DataGrid("select * from signs", "id", "signs");
            $dg -> set_dimension(400, 300);
            //$dg -> enable_export('EXCEL'); 
            $dg -> set_scroll(true);
            $dg -> enable_search(true);
            $dg -> set_col_hidden("id");
            //$dg -> set_col_img("img");

            $onSelectRow = <<<ONSELECTROW
            function (status, rowid) {
                if (lastActiveArea == "undefined" || lastActiveArea == null) {
                    alert("Please select a text area first");
                }
                else {
                    //alert(rowid);
                    d1 = $('#signs').jqGrid('getCol', 'imgCode', false);
                    lastActiveArea.innerText += "<br/>" + d1[rowid-1];
                }

            }
ONSELECTROW;


                //$dg->add_event("jqGridSelectRow", $onSelectRow);
                $dg -> add_event("jqGridDblClickRow", $onSelectRow);
                $dg -> display();
            ?>
            <a href="add_Signs.php">Add Signs</a>
        </div>
</div>

<?php include ("Includes/footer.php"); ?>